<template lang="pug">
el-dialog(title="创建调货单" :visible.sync="dialog" :before-close="close" append-to-body)
    goodslist(@getgoodsid="getgooditem")
    el-row
        el-col(:span="24")
            el-button(type="primary" @click="save") 保存
            el-button(type="primary" @click="open") 选择调货商品
        el-col.mgt(:span="24")
            el-col(:span="2")
                p 调货日期
            el-col(:span="10")
                el-date-picker(v-model="datetime" type="date" placeholder="选择日期(必填)")
    el-row
        el-col(:span="24")
            el-table(:data="td" style="width: 100%" height="350" v-loading="loading" element-loading-text="数据加载中...")
                el-table-column(prop="goodsId" label="商品编号")
                el-table-column(prop="name" label="商品名称")
                el-table-column(prop="spec" label="单位")
                el-table-column(label="单价/元(成本价)")
                    template(slot-scope="scope")
                        el-input(type="number" v-model="scope.row.prices" placeholder="必填")
                el-table-column(label="总金额")
                    template(slot-scope="scope")
                        el-input(type="number" v-model="scope.row.countprice" placeholder="必填")
                el-table-column(label="调货量")
                    template(slot-scope="scope")
                        el-input(type="number" v-model="scope.row.adjustNum" placeholder="必填")
                el-table-column(prop="usernames" label="调货方")
                el-table-column(label="收货方")
                    template(slot-scope="scope")
                        el-select(v-model="scope.row.inName" placeholder="收货方(必填)")
                            el-option(v-for="item in opt" :label="item.shopName" :key="item.shopId" :value="item.shopId")
                el-table-column(label="备注")
                    template(slot-scope="scope")
                        el-input(type="text" v-model="scope.row.remark" placeholder="选填")
                el-table-column(label="操作")
                    template(slot-scope="scope")
                        el-button(size="mini" type="danger" @click="del(scope.$index, scope.row)") 删除
</template>
<script>
import moment from 'moment'
import goodslist from 'components/GoodsLists'
export default{
    data(){
        return{
            dialog:false,
            loading:false,
            datetime:'',
            td:[],
            opt:[],
            inName:'',
            outName:''
        }
    },
    components:{
        goodslist
    },
    computed:{
        getstate(){
            return this.$store.state.creatediaohuoclass;
        }
    },
    watch:{
        getstate(val){//获取打开弹窗并请求店铺数据
            var _this=this
            axios.get('/shop/listShop.do').then(r=>{
                if(r.data.code!=200){
                    _this.$message.error(r.data.message)
                    return
                }
                _this.opt=[].concat(r.data.response.shopList)
            })
            this.dialog=val
            if(!this.$store.state.shiro){
                if(this.$store.state.TOPshopID){
                    this.shopID=this.$store.state.TOPshopID
                }
            }
        }
    },
    methods:{
        checkall(){//遍历所有填写的地方,效验
            if(!this.datetime){
                this.$message.error('调货时间不能为空')
                return false
            }
            for(var i=0;i<this.td.length;i++){
                if(!this.td[i].prices){
                    this.$message.error('单价不能为空')
                    return false
                }
                if(this.td[i].prices<=0){
                    this.$message.error('单价必须大于0')
                    return false
                }
                if(!this.td[i].adjustNum){
                    this.$message.error('调货量不能为空')
                    return false
                }
                if(this.td[i].adjustNum<=0){
                    this.$message.error('调货量必须大于0')
                    return false
                }
                if(!this.td[i].inName){
                    this.$message.error('收货方不能为空')
                    return false
                }
            }
            return true
        },
        checkacllback(arr){//遍历请求的返回值
            for(var i=0;i<arr.length;i++){
                if(arr[i].data.code!=200){
                    this.$message.error(arr[i].data.message)
                    return false
                }
                return true
            }
        },
        save(){//保存
            var _this=this
            if(this.$store.state.TOPshopID){
                
            }else{
                _this.$message.info('权限有误,不能添加')
                return
            }
            if(!this.checkall()){
                return
            }
            this.$confirm('创建后无法修改,是否确定?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                var axiosList=new Array()
                for(var i=0;i<_this.td.length;i++){
                    var pp=new URLSearchParams()
                    pp.append('goodId',_this.td[i].goodsId)
                    pp.append('goodName',_this.td[i].name)
                    pp.append('spec',_this.td[i].spec)
                    pp.append('price',_this.td[i].prices)
                    pp.append('adjustNum',_this.td[i].adjustNum)
                    pp.append('adjustTime',moment(_this.datetime).format("YYYY-MM-DD HH:mm:ss"))
                    pp.append('outShopId',_this.$store.state.TOPshopID)
                    pp.append('outShopName',_this.$store.state.TOPshopName)
                    pp.append('inShopId',_this.td[i].inName)
                    pp.append('inShopName',_this.getshopname(_this.td[i].inName))
                    pp.append('shopId',_this.$store.state.TOPshopID)
                    if(_this.td[i].remark){
                        pp.append('remark',_this.td[i].remark)
                    }
                    axiosList.push(axios.get('/addAdjust.do',{params:pp}))//此处axiosList获取到的就是请求返回的数据
                }

                axios.all(axiosList).then(r=>{
                    // console.log('axios.callback',r)
                    if(_this.checkacllback(r)){
                        _this.$message.success('调货成功')
                        _this.$store.state.creatediaohuo=false
                        _this.close()
                    }else{
                        return
                    }
                })

            }).catch(() => {
                return
            });

        },
        del(i,r){//删除一行
            this.td.splice(i,1)
        },
        open(){//打开商品选择
            this.$store.commit('ChangeWxGood')
        },
        close(){//清空数据,关闭
            this.td=[]
            this.datetime=''
            this.inName=0
            this.outName=0
            this.$store.state.creatediaohuoclass=false
        },
        getgooditem(m){//获取单个商品的请求信息
            var _this=this
            this.loading=true
            for(var i=0;i<m.length;i++){
                var pp=new URLSearchParams()
                pp.append('xcxGoodsId',m[i].goodsId)
                axios.get('/getXcxGoodsById.do',{params:pp}).then(r=>{
                    // console.log('getXcxGoodsById',r)
                    if(r.data.code==200){
                        _this.td.push(r.data.response.xcxGoods)
                        for(var j=0;j<_this.td.length;j++){
                            _this.td[j].usernames=_this.$store.state.TOPshopName
                        }
                    }else{
                        _this.$message.error(r.data.message)
                        _this.loading=false
                    }
                })
            }
            _this.loading=false
        },
        getshopname(d){//shopid转店铺名称
             
            // console.log('getshopname',d)
            // console.log('getshopname',this.opt)
            for(var i=0;i<this.opt.length;i++){
                if(d==this.opt[i].shopId){
                    // console.log('getshopname',this.opt[i])
                    return this.opt[i].shopName
                }
            }
        }
    }
} 
</script>
<style scoped>
.mgt{
    margin-top: 20px;
}
</style>
